package com.zimi.service;

import com.zimi.mapper.WfFormMapper;
import com.zimi.model.WfForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: lmc
 * @Date: 2019/3/27 10:33
 * @Description:
 */
@Service
public class WfFormService {

    @Autowired
    private WfFormMapper wfFormMapper;


    public int deleteByPrimaryKey(Integer id) {
        return wfFormMapper.deleteByPrimaryKey(id);
    }


    public int insert(WfForm wfForm) {
        return wfFormMapper.insert(wfForm);
    }


    public int insertSelective(WfForm wfForm) {
        WfForm wf = new WfForm();
        wf.setModuleId(wfForm.getModuleId());
        Integer orderNum = wfFormMapper.selectMaxOrderNum(wf);
        if (orderNum == null) {
            orderNum = 1;
        } else {
            orderNum = orderNum + 1;
        }
        wfForm.setOrderNum(orderNum);
        return wfFormMapper.insertSelective(wfForm);
    }


    public WfForm selectByPrimaryKey(Integer id) {
        return wfFormMapper.selectByPrimaryKey(id);
    }


    public int updateByPrimaryKeySelective(WfForm wfForm) {
        return wfFormMapper.updateByPrimaryKeySelective(wfForm);
    }


    public int updateByPrimaryKey(WfForm wfForm) {
        return wfFormMapper.updateByPrimaryKey(wfForm);
    }


    public List<WfForm> selectAll(WfForm wfForm) {
        return wfFormMapper.selectAll(wfForm);
    }

    public String moveUp(int id) {
        WfForm wfForm = wfFormMapper.selectByPrimaryKey(id);
        WfForm wf = new WfForm();
        wf.setModuleId(wfForm.getModuleId());
        wf.setActiveFlag(1);
        List<WfForm> list = wfFormMapper.selectAll(wf);

        int index = -1;
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getFormId() == wfForm.getFormId() || list.get(i).getFormId().equals(wfForm.getFormId())) {
                    index = i;
                }
            }
        }

        if (index == 0) {
            return "已到达顶部";
        } else {
            WfForm wfForm1 = list.get(index - 1);
            int sort = wfForm.getOrderNum();
            int sort1 = wfForm1.getOrderNum();

            wfForm.setOrderNum(sort1);
            wfFormMapper.updateByPrimaryKeySelective(wfForm);

            wfForm1.setOrderNum(sort);
            wfFormMapper.updateByPrimaryKeySelective(wfForm1);
        }

        return "上移成功";
    }


    public String moveDown(int id) {
        WfForm wfForm = wfFormMapper.selectByPrimaryKey(id);
        WfForm wf = new WfForm();
        wf.setModuleId(wfForm.getModuleId());
        wf.setActiveFlag(1);
        List<WfForm> list = wfFormMapper.selectAll(wf);

        int index = -1;
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getFormId() == wfForm.getFormId() || list.get(i).getFormId().equals(wfForm.getFormId())) {
                    index = i;
                }
            }
        }

        if (index == (list.size() - 1)) {
            return "已到达底部";
        } else {
            WfForm wfForm1 = list.get(index + 1);
            int sort = wfForm.getOrderNum();
            int sort1 = wfForm1.getOrderNum();

            wfForm.setOrderNum(sort1);
            wfFormMapper.updateByPrimaryKeySelective(wfForm);

            wfForm1.setOrderNum(sort);
            wfFormMapper.updateByPrimaryKeySelective(wfForm1);
        }

        return "下移成功";
    }
}
